
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>测试 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="comprehensions.html" />
    
    
    <link rel="prev" href="documentation.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">第一部分：基础</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="basics.html">
            
                <a href="basics.html">
            
                    
                    基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="collections.html">
            
                <a href="collections.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="enum.html">
            
                <a href="enum.html">
            
                    
                    Enum 模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="pattern-matching.html">
            
                <a href="pattern-matching.html">
            
                    
                    模式匹配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="control-structures.html">
            
                <a href="control-structures.html">
            
                    
                    控制语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="functions.html">
            
                <a href="functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="pipe-operator.html">
            
                <a href="pipe-operator.html">
            
                    
                    管道操作符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="modules.html">
            
                <a href="modules.html">
            
                    
                    模块(Module)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="mix.html">
            
                <a href="mix.html">
            
                    
                    Mix
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="sigils.html">
            
                <a href="sigils.html">
            
                    
                    魔符(Sigil)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="documentation.html">
            
                <a href="documentation.html">
            
                    
                    文档模块
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.13" data-path="testing.html">
            
                <a href="testing.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="comprehensions.html">
            
                <a href="comprehensions.html">
            
                    
                    推导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="strings.html">
            
                <a href="strings.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="date-time.html">
            
                <a href="date-time.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="mix-tasks.html">
            
                <a href="mix-tasks.html">
            
                    
                    自定义Mix任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="iex-helpers.html">
            
                <a href="iex-helpers.html">
            
                    
                    IEx辅助函数
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第二部分：高级</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../advanced/erlang.html">
            
                <a href="../advanced/erlang.html">
            
                    
                    和Erlang互操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../advanced/error-handling.html">
            
                <a href="../advanced/error-handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../advanced/escripts.html">
            
                <a href="../advanced/escripts.html">
            
                    
                    可执行文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../advanced/concurrency.html">
            
                <a href="../advanced/concurrency.html">
            
                    
                    并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="../advanced/otp-concurrency.html">
            
                <a href="../advanced/otp-concurrency.html">
            
                    
                    OTP并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="../advanced/otp-supervisors.html">
            
                <a href="../advanced/otp-supervisors.html">
            
                    
                    OTP Supervisors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="../advanced/otp-distribution.html">
            
                <a href="../advanced/otp-distribution.html">
            
                    
                    OTP 分布式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="../advanced/metaprogramming.html">
            
                <a href="../advanced/metaprogramming.html">
            
                    
                    元编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="../advanced/umbrella-projects.html">
            
                <a href="../advanced/umbrella-projects.html">
            
                    
                    Umbrella Projects
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="../advanced/typespec.html">
            
                <a href="../advanced/typespec.html">
            
                    
                    Specifications and types
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.11" data-path="../advanced/behaviours.html">
            
                <a href="../advanced/behaviours.html">
            
                    
                    行为
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.12" data-path="../advanced/gen-stage.html">
            
                <a href="../advanced/gen-stage.html">
            
                    
                    GenStage
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13" data-path="../advanced/protocols.html">
            
                <a href="../advanced/protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.14" data-path="../advanced/nerves.html">
            
                <a href="../advanced/nerves.html">
            
                    
                    Nerves
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第三部分：ECTO</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../ecto/basics.html">
            
                <a href="../ecto/basics.html">
            
                    
                    Basics
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../ecto/changesets.html">
            
                <a href="../ecto/changesets.html">
            
                    
                    Changesets
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../ecto/associations.html">
            
                <a href="../ecto/associations.html">
            
                    
                    关联关系
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../ecto/querying.html">
            
                <a href="../ecto/querying.html">
            
                    
                    查询
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第四部分：专题</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../specifics/plug.html">
            
                <a href="../specifics/plug.html">
            
                    
                    Plug
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../specifics/eex.html">
            
                <a href="../specifics/eex.html">
            
                    
                    嵌入的 Elixir(EEx)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="../specifics/ets.html">
            
                <a href="../specifics/ets.html">
            
                    
                    Erlang 项式存储(ETS)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="../specifics/mnesia.html">
            
                <a href="../specifics/mnesia.html">
            
                    
                    Mnesia 数据库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="../specifics/debugging.html">
            
                <a href="../specifics/debugging.html">
            
                    
                    调试
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第五部分：程序库</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../libraries/guardian.html">
            
                <a href="../libraries/guardian.html">
            
                    
                    Guardian(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../libraries/poolboy.html">
            
                <a href="../libraries/poolboy.html">
            
                    
                    Poolboy
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../libraries/benchee.html">
            
                <a href="../libraries/benchee.html">
            
                    
                    Benchee
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="../libraries/bypass.html">
            
                <a href="../libraries/bypass.html">
            
                    
                    Bypass
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="../libraries/distillery.html">
            
                <a href="../libraries/distillery.html">
            
                    
                    Distillery(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="../libraries/stream-data.html">
            
                <a href="../libraries/stream-data.html">
            
                    
                    StreamData
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >测试</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <p>&#x6D4B;&#x8BD5;&#x662F;&#x8F6F;&#x4EF6;&#x5F00;&#x53D1;&#x91CD;&#x8981;&#x7684;&#x4E00;&#x90E8;&#x5206;&#xFF0C;&#x8FD9;&#x8282;&#x8BFE;&#x6211;&#x4EEC;&#x4F1A;&#x8BB2;&#x4E00;&#x4E0B;&#x5982;&#x4F55;&#x4F7F;&#x7528; ExUnit &#x6D4B;&#x8BD5; Elixir &#x4EE3;&#x7801;&#xFF0C;&#x4EE5;&#x53CA;&#x6D4B;&#x8BD5;&#x4E2D;&#x7684;&#x6700;&#x4F73;&#x5B9E;&#x8DF5;&#x65B9;&#x6CD5;&#x3002;</p>
<h2 id="exunit">ExUnit</h2>
<p>Elixir &#x81EA;&#x5E26;&#x7684;&#x6D4B;&#x8BD5;&#x6846;&#x67B6;&#x662F; ExUnit&#xFF0C;&#x5B83;&#x5305;&#x62EC;&#x7684;&#x529F;&#x80FD;&#x8DB3;&#x591F;&#x6211;&#x4EEC;&#x5145;&#x5206;&#x6D4B;&#x8BD5;&#x81EA;&#x5DF1;&#x7684;&#x4EE3;&#x7801;&#x3002;&#x5728;&#x7EE7;&#x7EED;&#x8BB2;&#x89E3;&#x4E4B;&#x524D;&#xFF0C;&#x6709;&#x4E00;&#x70B9;&#x8981;&#x6CE8;&#x610F;&#xFF1A;&#x6D4B;&#x8BD5;&#x662F;&#x901A;&#x8FC7; Elixir &#x811A;&#x672C;&#x6765;&#x6267;&#x884C;&#x7684;&#xFF0C;&#x6240;&#x4EE5;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x7684;&#x540E;&#x7F00;&#x540D;&#x5FC5;&#x987B;&#x662F; <code>.exs</code>&#x3002;&#x5728;&#x8FD0;&#x884C;&#x6D4B;&#x8BD5;&#x4E4B;&#x524D;&#xFF0C;&#x6211;&#x4EEC;&#x8981;&#x5148;&#x7528; <code>ExUnit.start()</code> &#x6765;&#x542F;&#x52A8; ExUnit&#xFF0C;&#x8FD9;&#x4E00;&#x822C;&#x5728; <code>test/test_helper.exs</code> &#x5DF2;&#x7ECF;&#x5E2E;&#x6211;&#x4EEC;&#x505A;&#x4E86;&#x3002;</p>
<p>&#x4E0A;&#x8282;&#x8BFE;&#x6211;&#x4EEC;&#x81EA;&#x52A8;&#x751F;&#x6210;&#x7684;&#x793A;&#x4F8B;&#x9879;&#x76EE;&#x4E2D;&#xFF0C;mix &#x5DF2;&#x7ECF;&#x5E2E;&#x6211;&#x4EEC;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x6D4B;&#x8BD5;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x5728; <code>test/example_test.exs</code> &#x6587;&#x4EF6;&#x4E2D;&#x770B;&#x5230;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">ExampleTest</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> ExUnit.Case
  doctest Example

  test <span class="hljs-string">&quot;greets the world&quot;</span> <span class="hljs-keyword">do</span>
    assert Example.hello() == <span class="hljs-symbol">:world</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x6267;&#x884C; <code>mix test</code> &#x547D;&#x4EE4;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x80FD;&#x8FD0;&#x884C;&#x9879;&#x76EE;&#x7684;&#x6D4B;&#x8BD5;&#x4E86;&#xFF0C;&#x6267;&#x884C;&#x4E4B;&#x540E;&#x4F1A;&#x770B;&#x5230;&#x7C7B;&#x4F3C;&#x4E0B;&#x9762;&#x7684;&#x8F93;&#x51FA;&#xFF1A;</p>
<pre><code class="lang-shell">..

Finished in 0.03 seconds
2 tests, 0 failures
</code></pre>
<h3 id="&#x65AD;&#x8A00;&#xFF08;assert&#xFF09;">&#x65AD;&#x8A00;&#xFF08;assert&#xFF09;</h3>
<p>&#x5982;&#x679C;&#x4F60;&#x4E4B;&#x524D;&#x5199;&#x8FC7;&#x6D4B;&#x8BD5;&#xFF0C;&#x90A3;&#x5BF9; <code>assert</code> &#x5DF2;&#x7ECF;&#x5F88;&#x719F;&#x6089;&#x4E86;&#xFF0C;&#x5728;&#x6709;&#x4E9B;&#x6D4B;&#x8BD5;&#x6846;&#x67B6;&#x4E2D;&#xFF0C; <code>should</code> &#x6216;&#x8005; <code>expect</code> &#x7684;&#x529F;&#x80FD;&#x548C; <code>assert</code> &#x4E00;&#x6837;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x5728;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x4E2D;&#x4F7F;&#x7528; <code>assert</code> &#x5B8F;&#x6765;&#x68C0;&#x67E5;&#x8868;&#x8FBE;&#x5F0F;&#x4E3A;&#x771F;&#xFF0C;&#x5982;&#x679C;&#x8868;&#x8FBE;&#x5F0F;&#x4E0D;&#x4E3A;&#x771F;&#xFF0C;&#x5C31;&#x4F1A;&#x629B;&#x51FA;&#x5F02;&#x5E38;&#xFF0C;&#x6D4B;&#x8BD5;&#x4E5F;&#x5C31;&#x5931;&#x8D25;&#x4E86;&#x3002;&#x4E3A;&#x4E86;&#x770B;&#x770B;&#x5931;&#x8D25;&#x7684;&#x60C5;&#x51B5;&#xFF0C;&#x6211;&#x4EEC;&#x4FEE;&#x6539;&#x4E00;&#x4E0B;&#x9879;&#x76EE;&#x7684;&#x6D4B;&#x8BD5;&#xFF0C;&#x7136;&#x540E;&#x8FD0;&#x884C; <code>mix test</code>:</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">ExampleTest</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> ExUnit.Case
  doctest Example

  test <span class="hljs-string">&quot;greets the world&quot;</span> <span class="hljs-keyword">do</span>
    assert Example.hello() == <span class="hljs-symbol">:word</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x8FD9;&#x6B21;&#x770B;&#x5230;&#x7684;&#x8F93;&#x51FA;&#x548C;&#x4E4B;&#x524D;&#x5927;&#x4E0D;&#x76F8;&#x540C;&#xFF1A;</p>
<pre><code class="lang-shell">  1) test greets the world (ExampleTest)
     test/example_test.exs:5
     Assertion with == failed
     code:  assert Example.hello() == :word
     left:  :world
     right: :word
     stacktrace:
       test/example_test.exs:6 (test)

.

Finished in 0.03 seconds
2 tests, 1 failures
</code></pre>
<p>ExUnit &#x4F1A;&#x544A;&#x8BC9;&#x6211;&#x4EEC;&#x9519;&#x8BEF;&#x65AD;&#x8A00;&#x51FA;&#x73B0;&#x7684;&#x884C;&#x6570;&#xFF0C;&#x671F;&#x671B;&#x7684;&#x503C;&#x662F;&#x4EC0;&#x4E48;&#xFF0C;&#x5B9E;&#x9645;&#x8FD0;&#x884C;&#x7684;&#x503C;&#x662F;&#x4EC0;&#x4E48;&#x3002;</p>
<h3 id="refute">refute</h3>
<p><code>refute</code> &#x548C; <code>assert</code> &#x7684;&#x5173;&#x7CFB;&#x5C31;&#x50CF; <code>unless</code> &#x548C; <code>if</code> &#x7684;&#x5173;&#x7CFB;&#x4E00;&#x6837;&#xFF0C;&#x5982;&#x679C;&#x8981;&#x4FDD;&#x8BC1;&#x67D0;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#x4E00;&#x5B9A;&#x662F;&#x5047;&#x7684;&#xFF0C;&#x8BF7;&#x4F7F;&#x7528; <code>refute</code>&#x3002;</p>
<h3 id="assertraise">assert_raise</h3>
<p>&#x6709;&#x65F6;&#x5019;&#x4F1A;&#x65AD;&#x8A00;&#x67D0;&#x4E2A;&#x9519;&#x8BEF;&#x88AB;&#x629B;&#x51FA;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>assert_raise</code> &#x505A;&#x8FD9;&#x4EF6;&#x4E8B;&#xFF0C;&#x6211;&#x4EEC;&#x4F1A;&#x5728;&#x540E;&#x9762; Plug &#x7684;&#x8BFE;&#x7A0B;&#x4E2D;&#x770B;&#x5230; <code>assert_raise</code> &#x7684;&#x4F8B;&#x5B50;&#x3002;</p>
<h3 id="assertreceive">assert_receive</h3>
<p>&#x5728; Elixir &#x8BED;&#x8A00;&#x4E2D;&#xFF0C;&#x4F1A;&#x6709;&#x5F88;&#x591A; actors/processes &#x4E4B;&#x95F4;&#x4E92;&#x76F8;&#x53D1;&#x9001;&#x6D88;&#x606F;&#xFF0C;&#x56E0;&#x6B64;&#x6709;&#x65F6;&#x5019;&#x9700;&#x8981;&#x6D4B;&#x8BD5;&#x67D0;&#x4E9B;&#x6D88;&#x606F;&#x662F;&#x5426;&#x88AB;&#x53D1;&#x9001;&#x3002;&#x56E0;&#x4E3A; ExUnit &#x662F;&#x8FD0;&#x884C;&#x5728;&#x81EA;&#x5DF1;&#x7684; process &#x7684;&#xFF0C;&#x56E0;&#x6B64;&#x53EF;&#x4EE5;&#x50CF;&#x5176;&#x4ED6; process &#x90A3;&#x6837;&#x63A5;&#x53D7;&#x6D88;&#x606F;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>assert_received</code> &#x6765;&#x65AD;&#x8A00;&#x6D88;&#x606F;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">SendingProcess</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">run</span></span>(pid) <span class="hljs-keyword">do</span>
    send(pid, <span class="hljs-symbol">:ping</span>)
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>

<span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">TestReceive</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> ExUnit.Case

  test <span class="hljs-string">&quot;receives ping&quot;</span> <span class="hljs-keyword">do</span>
    SendingProcess.run(<span class="hljs-keyword">self</span>())
    assert_received <span class="hljs-symbol">:ping</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p><code>assert_reveived</code> &#x5E76;&#x4E0D;&#x4F1A;&#x7B49;&#x5F85;&#x6D88;&#x606F;&#xFF0C;&#x5982;&#x679C;&#x9700;&#x8981;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>assert_reveive</code> &#x5E76;&#x6307;&#x5B9A;&#x8D85;&#x65F6;&#x65F6;&#x95F4;&#x3002;</p>
<h3 id="captureio-&#x548C;-capturelog">capture_io &#x548C; capture_log</h3>
<p>&#x4F7F;&#x7528; <code>ExUnit.CaptureIO</code> &#x53EF;&#x4EE5;&#x5728;&#x4E0D;&#x6539;&#x53D8;&#x539F;&#x6765;&#x5E94;&#x7528;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x6355;&#x83B7;&#x5E94;&#x7528;&#x7684;&#x8F93;&#x51FA;&#x3002;&#x53EA;&#x8981;&#x628A;&#x751F;&#x6210;&#x8F93;&#x51FA;&#x7684;&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x4F20;&#x8FDB;&#x53BB;&#x5C31;&#x884C;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">OutputTest</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> ExUnit.Case
  import ExUnit.CaptureIO

  test <span class="hljs-string">&quot;outputs Hello World&quot;</span> <span class="hljs-keyword">do</span>
    assert capture_io(<span class="hljs-keyword">fn</span> -&gt; IO.puts(<span class="hljs-string">&quot;Hello World&quot;</span>) <span class="hljs-keyword">end</span>) == <span class="hljs-string">&quot;Hello World\n&quot;</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p><code>ExUnit.CaptureLog</code> &#x5C31;&#x662F;&#x6355;&#x83B7; <code>Logger</code> &#x7684;&#x8F93;&#x51FA;&#x3002;</p>
<h2 id="test-&#x914D;&#x7F6E;">Test &#x914D;&#x7F6E;</h2>
<p>&#x6709;&#x65F6;&#x5019;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5728;&#x6267;&#x884C;&#x771F;&#x6B63;&#x7684;&#x6D4B;&#x8BD5;&#x4E4B;&#x524D;&#x505A;&#x4E00;&#x4E0B;&#x914D;&#x7F6E;&#x5DE5;&#x4F5C;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>setup</code> &#x548C; <code>setup_all</code> &#x8FD9;&#x4E24;&#x4E2A;&#x5B8F;&#x3002;<code>setup</code> &#x5728;&#x67D0;&#x4E2A;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x4E4B;&#x524D;&#x90FD;&#x4F1A;&#x88AB;&#x8FD0;&#x884C;&#xFF0C;<code>setup_all</code> &#x53EA;&#x4F1A;&#x5728;&#x6574;&#x5957;&#x6D4B;&#x8BD5;&#x4E4B;&#x524D;&#x8FD0;&#x884C;&#x4E00;&#x6B21;&#x3002;&#x5B83;&#x4EEC;&#x4E24;&#x4E2A;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x662F;&#x5143;&#x7EC4;&#xFF1A;<code>{:ok, state}</code>&#xFF0C;&#x5176;&#x4E2D; <code>state</code> &#x53EF;&#x4EE5;&#x518D;&#x540E;&#x7EED;&#x7684;&#x6D4B;&#x8BD5;&#x4E2D;&#x88AB;&#x4F7F;&#x7528;&#x3002;</p>
<p>&#x4E3A;&#x4E86;&#x65B9;&#x4FBF;&#x4E3E;&#x4F8B;&#x5B50;&#xFF0C;&#x6211;&#x4EEC;&#x628A;&#x6D4B;&#x8BD5;&#x4EE3;&#x7801;&#x4FEE;&#x6539;&#x4E00;&#x4E0B;&#xFF0C;&#x6DFB;&#x52A0;&#x4E0A; <code>setup_all</code>&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">ExampleTest</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-keyword">use</span> ExUnit.Case
  doctest Example

  setup_all <span class="hljs-keyword">do</span>
    {<span class="hljs-symbol">:ok</span>, <span class="hljs-symbol">number:</span> <span class="hljs-number">2</span>}
  <span class="hljs-keyword">end</span>

  test <span class="hljs-string">&quot;the truth&quot;</span>, state <span class="hljs-keyword">do</span>
    assert <span class="hljs-number">1</span> + <span class="hljs-number">1</span> == state[<span class="hljs-symbol">:number</span>]
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<h2 id="mocking">Mocking</h2>
<p>Elixir &#x5BF9; mocking &#x7684;&#x5EFA;&#x8BAE;&#x662F;&#xFF1A;&#x4E0D;&#x8981;&#x4F7F;&#x7528; mocking&#x3002;&#x4F60;&#x53EF;&#x80FD;&#x4E0D;&#x81EA;&#x89C9;&#x5730;&#x60F3;&#x4F7F;&#x7528; mock&#xFF0C;&#x4F46;&#x662F;&#x5B83;&#x4E0D;&#x88AB; Elixir &#x793E;&#x533A;&#x63A8;&#x8350;&#x662F;&#x6709;&#x5145;&#x5206;&#x7406;&#x7531;&#x7684;&#x3002;&#x5982;&#x679C;&#x4F60;&#x4EE3;&#x7801;&#x9075;&#x5FAA;&#x826F;&#x597D;&#x7684;&#x8BBE;&#x8BA1;&#x539F;&#x5219;&#xFF0C;&#x5B83;&#x4EEC;&#x4F1A;&#x5F88;&#x5BB9;&#x6613;&#x6D4B;&#x8BD5;&#x3002;</p>
<p><a href="http://blog.plataformatec.com.br/2015/10/mocks-and-explicit-contracts/" target="_blank">&#x8FD9;&#x91CC;&#x8FD9;&#x7BC7;&#x6587;&#x7AE0;</a>&#x66F4;&#x8BE6;&#x7EC6;&#x7684;&#x8BA8;&#x8BBA;&#x3002;&#x5176;&#x4E2D;&#x7684;&#x8981;&#x70B9;&#x662F;&#xFF0C;&#x5B9A;&#x4E49;&#x5BF9;&#x5916;&#x4EE3;&#x7801;&#x7684;&#x63A5;&#x53E3;&#xFF08;&#x884C;&#x4E3A;&#xFF09;&#xFF0C;&#x7136;&#x540E;&#x5728;&#x6D4B;&#x8BD5;&#x65F6;&#x4F7F;&#x7528;&#x6A21;&#x62DF;&#x7684;&#x5B9E;&#x73B0;&#x6BD4;&#x76F4;&#x63A5;&#x5728;&#x6D4B;&#x8BD5;&#x4EE3;&#x7801;&#x4E2D; mock &#x4E00;&#x4E2A;&#x4F9D;&#x8D56;&#x8981;&#x597D;&#x3002;</p>
<p>&#x8981;&#x5207;&#x6362;&#x8FD9;&#x4E2A;&#x884C;&#x4E3A;&#x7684;&#x5B9E;&#x73B0;&#xFF0C;&#x9996;&#x9009;&#x7684;&#x65B9;&#x5F0F;&#x662F;&#x628A;&#x5B9E;&#x73B0;&#x884C;&#x4E3A;&#x7684;&#x6A21;&#x5757;&#x540D;&#x5F53;&#x4F5C;&#x53C2;&#x6570;&#x4F20;&#x9012;&#xFF0C;&#x5E76;&#x8BBE;&#x5B9A;&#x9ED8;&#x8BA4;&#x503C;&#x3002;&#x5982;&#x679C;&#x8FD9;&#x6837;&#x4E0D;&#x884C;&#x7684;&#x8BDD;&#xFF0C;&#x53EF;&#x4EE5;&#x7528;&#x914D;&#x7F6E;&#x7684;&#x65B9;&#x5F0F;&#x6765;&#x5B9E;&#x73B0;&#x3002;&#x8FD9;&#x6837;&#xFF0C;&#x4F60;&#x4E0D;&#x9700;&#x8981;&#x4EFB;&#x4F55; Mocking &#x7684;&#x5E93;&#xFF0C;&#x53EA;&#x9700;&#x8981;&#x7528;&#x5230;&#x884C;&#x4E3A;&#x548C;&#x56DE;&#x8C03;&#x5C31;&#x53EF;&#x4EE5;&#x4E86;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="documentation.html" class="navigation navigation-prev " aria-label="Previous page: 文档模块">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="comprehensions.html" class="navigation navigation-next " aria-label="Next page: 推导">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"version":"1.1.1","title":"测试","level":"1.13","depth":1,"next":{"title":"推导","level":"1.14","depth":1,"path":"lessons/basics/comprehensions.md","ref":"lessons/basics/comprehensions.md","articles":[]},"previous":{"title":"文档模块","level":"1.12","depth":1,"path":"lessons/basics/documentation.md","ref":"lessons/basics/documentation.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"lessons/basics/testing.md","mtime":"2019-11-05T02:25:51.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-11-05T03:01:22.004Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

